<template>
	<view v-if="pageShow == '已通过'">
		<homeCom @changeFag="changeFag" :flag="flag" ref="home" @handleAuth="getCode" @changeTab="change"
			v-if="current == 0" />
		<template v-if="vuex_token8">
			<scan v-if="current == 1" />
			<mine @send="change" v-else-if="current == 2" />
		</template>
		<u-tabbar icon-size="48" :border-top="false" :hide-tab-bar="false" active-color="#fbc547" inactive-color="#87592a"
			@change="change" bg-color="#191917" v-model="current" :list="list">
		</u-tabbar>
	</view>
</template>

<script>
import homeCom from "../home/index.vue"
import scan from "../scan/index.vue"
import mine from "../mine/index.vue"

export default {
	components: {
		homeCom,
		mine,
		scan
	},
	data() {
		return {
			domain: '',
			info: null,
			flag: 1,
			code: '',
			shareCode: '',
			pageShow: '已通过',
			list: [{
				iconPath: "/static/you-magic/images/home.png",
				selectedIconPath: "/static/you-magic/images/ahome.png",
				text: '首页',
				customIcon: false,
			},
			{
				iconPath: "/static/you-magic/images/caner.png",
				selectedIconPath: "/static/you-magic/images/acaner.png",
				text: '我要赚钱',
				midButton: true,
				customIcon: false,
			},
			{
				iconPath: "/static/you-magic/images/my.png",
				selectedIconPath: "/static/you-magic/images/amy.png",
				text: '个人中心',
				customIcon: false,
			}
			],
			current: 0
		}
	},
	onLoad() {
		// this.$u.api
		// 	.getConfig({
		// 		key: `stl_mini_audit_v4.0.0`,
		// 	})
		// 	.then((res) => {
		// 		this.pageShow = res.config

		// 	});

		this.addpv()
		// this.getdomain()

		if (this.vuex_token8) {
			console.log(this.vuex_token8,'我有token');
			this.Fnuseradd()
		} else {
			console.log(this.vuex_token8,'我么有openid');
			this.flag = 4
		}
	},
	methods: {
		changeFag(e) {
			this.flag = 2
		},

		addpv() {
			this.$u.api
				.pvadd()
				.then((res) => { });
		},

		getdomain() {
			this.$u.api
				.domain()
				.then((res) => {
					console.log(res, 777777);
				});
		},

		Fnuseradd() {

			// alert(this.vuex_shareCode8)
			this.$u.api
				.useradd({
					shareCode: this.vuex_shareCode8,
					nickName: this.vuex_nickname8,
					openid: this.vuex_openId8,
					avater: this.vuex_avatar8
				})
				.then((res) => {
					console.log(res, 'add接口返回的参数');

					// alert(JSON.stringify(res))
					// redPacketStatus  1是 2否
					let { redPacketAmt, promotionCode, redPacketStatus, availableRebateAmt, availableRedPacketAmt } = res.data

					this.$u.vuex("vuex_redPacketAmt8", redPacketAmt); //红包金额

					this.$u.vuex("vuex_availableRebateAmt8", availableRebateAmt); //佣金余额

					this.$u.vuex("vuex_promotionCode8", promotionCode); //海报

					this.$u.vuex("vuex_availableRedPacketAmt8", availableRedPacketAmt); //红包余额
					// redPacketStatus=2
					if (redPacketStatus == 1) {
						this.flag = 3
					} else if (redPacketStatus == 2) {
						this.flag = 1
					}
				});
		},

		getCode() {

			// this.code = this.getUrlCode().code; // 截取code
			

		

			console.log('我走进了 getCode 方法');
			


			// 如果没有code，则去请求
			if (!this.vuex_token8) {


				console.log('vuex_token8 没拿到值');

				const APPID = 'wx0b62237ffb22f010' //正式
				// const APPID = 'wxb041426f4c673fb9'  //测试

				// const callbackURL = this.domain
				// const callbackURL = encodeURIComponent(location.href)
				// const callbackURL = 'http://192.168.3.154:8081/mp/'

				this.$u.api
					.domain()
					.then((res) => {
						console.log(res.msg, 66666666666666);

						const callbackURL = res.msg + '?shareCode=' + this.shareCode || ''

						alert(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${callbackURL}&response_type=code&scope=snsapi_userinfo&forcePopup=true&forceSnapShot=false#wechat_redirect`)

						// console.log(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${callbackURL}&response_type=code&scope=snsapi_userinfo&forcePopup=true&forceSnapShot=false#wechat_redirect`);

						window.location.href =
								`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}&redirect_uri=${callbackURL}&response_type=code&scope=snsapi_base&forcePopup=true&forceSnapShot=false#wechat_redirect`

					});

			} else {

				this.FnsetHasLogin()

				
			}
		},



		FnsetHasLogin() {

			alert(this.getUrlCode().nickName)
			alert(this.getUrlCode().headImgUrl)
			alert(this.getUrlCode().openId)
			

			// this.code = this.getUrlCode().headImgUrl; // 截取code

			this.$u.vuex("vuex_avatar8", this.getUrlCode().nickName);
			this.$u.vuex("vuex_nickname8", this.getUrlCode().headImgUrl);
			this.$u.vuex("vuex_openId8", this.getUrlCode().openId);
			this.$u.vuex("vuex_token8", this.getUrlCode().openId);

			// this.shareCode = this.getUrlCode()?.shareCode //截取shareCode
			this.$u.vuex("vuex_shareCode8", this.getUrlCode().shareCode);

			this.Fnuseradd()



			// this.$u.api
			// 	.getUserInfo({
			// 		code: this.code
			// 	})
			// 	.then((res) => {
			// 		console.log(res.data, 'getUserInfo返回的参数');
			// 		let { headImgUrl, nickname, openId } = res.data
			// 		this.$u.vuex("vuex_avatar8", headImgUrl);
			// 		this.$u.vuex("vuex_nickname8", nickname);
			// 		this.$u.vuex("vuex_openId8", openId);
			// 		this.$u.vuex("vuex_token8", openId);


			// 		this.Fnuseradd()
			// 	});
		},
		getUrlCode() {
			// 截取url中的code方法
			var url = location.href; //获取打开的公众号的路径
			let winUrl = url;
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var str = url.substr(url.indexOf("?") + 1);
				var strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					var items = strs[i].split("=");
					theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
				}
			}
			return theRequest;
		},
		change(i) {
			if (this.vuex_token8) {
				this.current = i
			} else {
				if ([1, 2].includes(i)) {
					this.getCode()

				}
			}
		}
	},
}
</script>


<style scoped lang="scss">
/deep/ .u-tabbar__content {
	padding-bottom: env(safe-area-inset-bottom) !important; // 底部安全区
}

/deep/ .u-tabbar__content__item__text {
	margin-bottom: 3px !important;
}
</style>
